#include "bsp.h"


unsigned char RawData[5] = {0};
unsigned char RawData1[5] = {0};
#define SCALE_FACTOR 10 // 缩放因子，保留一位小数

void print_scaled_sensor_data(float pressure, float temperature, int pressure_scale, int temperature_scale) 
{
    float scaled_pressure = pressure * (float)pressure_scale;
    float scaled_temperature = temperature * (float)temperature_scale;
    float abs_scaled_pressure = fabsf(scaled_pressure);
    float abs_scaled_temperature = fabsf(scaled_temperature);
    int32_t int_pressure = (int32_t)roundf(abs_scaled_pressure);
    int32_t int_temperature = (int32_t)roundf(abs_scaled_temperature);
    //printf("Scaled Pressure: %d , -------------- Scaled Temperature: %d \r\n" , int_pressure, int_temperature);
}

int main( void )
{
    bsp_Init();
    bsp_StartAutoTimer(0, 1000);
    GPIO_Init(GPIOD, (GPIO_Pin_TypeDef)GPIO_PIN_4, GPIO_MODE_OUT_PP_HIGH_FAST);
    GPIO_Init(GPIOC, (GPIO_Pin_TypeDef)GPIO_PIN_6, GPIO_MODE_OUT_PP_HIGH_FAST);
    float current_pressure = 0.00f, current_temperature = 0.00f;   
    static long int_pressure = 0;
    static int int_temperature = 0;
    static char timeloop = 0;
    GPIO_WriteHigh(GPIOC , GPIO_PIN_6); 
    while (1)
    {
        if (bsp_CheckTimer(0))
        {
            GPIO_WriteReverse(GPIOD , GPIO_PIN_4);
            GPIO_WriteReverse(GPIOC , GPIO_PIN_6);  
            CalSensorData(&current_pressure, &current_temperature);
            int_pressure = current_pressure * 1000;
            int_temperature = current_temperature * 1000;
            printf("Pressure: %lu * 0.001 (kpa) \r\n" , int_pressure ); 
            printf("Temperature: %d * 0.001 (C) \r\n" ,int_temperature);
            
            
            if(int_pressure > 95837)
            {//当压力达到一定程度
                timeloop++;
            }else{
                timeloop = 0;
            }
            
            if(timeloop >= 10)
            {
                GPIO_WriteLow(GPIOC , GPIO_PIN_6); 
                GPIO_WriteHigh(GPIOC , GPIO_PIN_6);
            }
            
            
        }   
    }
}

/**
  * @brief Delay
  * @param nCount
  * @retval None
  */
void Delay(uint16_t nCount)
{
  /* Decrement nCount value */
  while (nCount != 0)
  {
    nCount--;
  }
}

#ifdef USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *   where the assert_param error has occurred.
  * @param file: pointer to the source file name
  * @param line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif
